//
//  NoticeListViewController.m
//  OA
//
//  Created by apple on 15-10-15.
//  Copyright (c) 2015年 net. All rights reserved.
//

#import "NoticeListViewController.h"
#import "AppDelegate.h"
#import "NoticeContentViewController.h"
#import "Constants.h"
@interface NoticeListViewController ()

@end

@implementation NoticeListViewController

NSMutableArray * noticeTitles;
NSMutableArray * tableMoreData;//下拉时现实的数据
NSUInteger dataNumber;//数据流量
BOOL _loadingMore;//加载状态
NSString * pageNo;
AppDelegate * appDelegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"通告标题";
    pageNo = @"1";
    noticeTitles = [[NSMutableArray alloc] init];
    tableMoreData = [[NSMutableArray alloc] init];
    NSDictionary * params = @{@"pageNo":pageNo};
    self.tableView.dataSource= self;
    self.tableView.delegate = self;
    //[self createTableFooter];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //noticeTitles = [NSArray arrayWithObjects:@"通告标题1",@"通告标题2",@"通告标题3",@"通告标题4",@"通告标题5", nil];
    appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.manager
     GET:url1
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         //noticeTitles = responseObject;
         [noticeTitles addObjectsFromArray:responseObject];
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         [self.tableView reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"获取服务器响应出错！");
     }];
}

-(void) loadDataing{
    dataNumber = [noticeTitles count];
    int pageNoInt = [pageNo intValue];
    pageNoInt++;
    pageNo = [NSString stringWithFormat:@"%d",pageNoInt];//NSLog(@"%@",pageNo);
    NSDictionary * params = @{@"pageNo":pageNo};
    [appDelegate.manager
     GET:url1
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         //tableMoreData = responseObject;
         [tableMoreData addObjectsFromArray:responseObject];
         //         NSDictionary* dict = [notices objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         //             for(int x=0;x<[tableMoreData count];x++){
         //                [noticeTitles addObject:[tableMoreData objectAtIndex:x]];
         //
         //             }
         [noticeTitles addObjectsFromArray:tableMoreData];//NSLog(@"%lu",(unsigned long)[noticeTitles count]);
         [self.tableView reloadData];
         [self loadDataEnd];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"获取服务器响应出错！");
     }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return noticeTitles.count;
    //return 8;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"noticeTableCell" forIndexPath:indexPath];
    cell.layer.cornerRadius=12;
    cell.layer.masksToBounds=YES;
    UILabel * label = (UILabel*)[cell viewWithTag:1];
    label.text = [[noticeTitles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //    if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height-scrollView.frame.size.height)))
    //    {
    //        [self loadDataBegain];
    //    }
    //   NSLog(@"%f",scrollView.contentOffset.y);//上拉为127.5下拉为-137
    //    NSLog(@"%f,%f",scrollView.contentSize.height,scrollView.frame.size.height);//568,600
    
    if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height)))
    {
        [self loadDataBegain];
    }
    
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell * cell = (UITableViewCell*)sender;
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    NoticeContentViewController * noticeContentViewController = (NoticeContentViewController*)segue.destinationViewController;
    noticeContentViewController.noticeId = [[noticeTitles objectAtIndex:indexPath.row] objectForKey:@"id"];
}
// 创建表格底部
//- (void) createTableFooter
//{
//    self.tableView.tableFooterView = nil;
//    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 40.0f)];
//    UILabel *loadMoreText = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 116.0f, 40.0f)];
//    [loadMoreText setCenter:tableFooterView.center];
//    [loadMoreText setFont:[UIFont fontWithName:@"Helvetica Neue" size:14]];
//    [loadMoreText setText:@"上拉显示更多数据"];
//    [loadMoreText setTextAlignment:NSTextAlignmentCenter];
//    [tableFooterView addSubview:loadMoreText];
//    
//    self.tableView.tableFooterView = tableFooterView;
//}

-(void) loadDataBegain{
    if(_loadingMore==NO){
        _loadingMore = YES;
//        UIActivityIndicatorView *tableFooterActivityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(75.0f, 10.0f, 20.0f, 20.0f)];
//        [tableFooterActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
//        [tableFooterActivityIndicator startAnimating];
//        [self.tableView.tableFooterView addSubview:tableFooterActivityIndicator];
//        
        [self loadDataing];
    }
}

-(void) loadDataEnd{
    _loadingMore = NO;
    //[self createTableFooter];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
